package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.constants.Constants;
import com.tanhua.model.dto.FreezeDto;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/5
 */
@Service
public class UserFreezeService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 判断用户是否被冻结
     * @param state  冻结范围 1(登录)/2(发言)/3(发布动态)
     * @param userId 用户id
     */
    public void checkUserStatus(String state, Long userId) {
        String key = Constants.USER_FREEZE + userId.toString();
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotEmpty(value)) {
            FreezeDto freezeDto = JSON.parseObject(value, FreezeDto.class);
            String freezingRange = freezeDto.getFreezingRange().toString();
            if (state.equals(freezingRange)) {
                switch (state) {
                    case "1": {
                        throw new BusinessException(ErrorResult.freezeLoginError());
                    }
                    case "2": {
                        throw new BusinessException(ErrorResult.freezeCommentError());
                    }
                    case "3": {
                        throw new BusinessException(ErrorResult.freezeMovementError());
                    }
                    default:
                        break;
                }
            }
        }
    }
}
